package com.lin.utils;

import com.lin.constant.ResultCode;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * @program: blog
 * @description: result 全局结果返回
 * @author: Lin
 * @create: 2020-04-03 12:28
 **/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Result<T> implements Serializable {

    private Integer code;
    private String message;
    private T data;

    public void setResultCode(ResultCode resultCode){
        this.code = resultCode.getCode();
        this.message = resultCode.getMessage();
    }


    /**
     * 返回成功
     * @return
     */
    public static Result success(){
        Result result = new Result();
        result.setResultCode(ResultCode.SUCCESS);
        return result;
    }

    /**
     * 返回成功
     * @return
     */
    public static Result success(Object data){
        Result result = new Result();
        result.setResultCode(ResultCode.SUCCESS);
        result.setData(data);
        return result;
    }

    /**
     * 返回成功
     * @return
     */
    public static Result successMessage(String message){
        Result result = new Result();
        result.setResultCode(ResultCode.SUCCESS);
        result.setData(null);
        result.setMessage(message);
        return result;
    }


    /**
     * 返回失败
     * @param resultCode
     * @return
     */
    public static Result failure(ResultCode resultCode){
        Result result = new Result();
        result.setResultCode(resultCode);
        return result;
    }

    /**
     * 返回失败
     * @param message
     * @return
     */
    public static Result failureMessage(String message){
        Result result = new Result();
        result.setResultCode(ResultCode.FAIL);
        result.setData(null);
        result.setMessage(message);
        return result;
    }

    /**
     * 返回失败
     * @param resultCode
     * @return
     */
    public static Result failure(ResultCode resultCode,Object data){
        Result result = new Result();
        result.setResultCode(resultCode);
        result.setData(data);
        return result;
    }
}
